<?php


namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Models\Notification;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class NotificationController extends BaseController
{

    public function index(Request $request)
    {
        $this->validate($request, [
           'type' => 'required'
        ]);
        $user_id = $this->getUserId();
        $type = $request->input('type');
        $arr_type = explode(",", $type);
        $list = Notification::whereIn('type', $arr_type)->with(['user' => function ($q) {
            $q->select('id', 'nickname', 'weixin', 'avatar_url');
        }, 'user.animal' => function ($q) {
            $q->select('user_id', 'title', 'birthday', 'category_title');
        }])
        ->where('is_send', 1)
        ->where(function ($q) use($user_id) {
            $q->where('entity_user_id', $user_id)->orWhere('entity_user_id', 0);
        })
        ->withCertain('comment', ['message', 'created_at'])
        ->withCertain('entity', ['file_path', 'type'])
        ->orderBy('updated_at', 'desc')
        ->paginate(\request('per_page', 20));

        $extra = null;
        if ($type == '5,6,8') {
            $arr = Redis::mget(['user_latest_fans_count_' . $user_id, 'user_latest_star_count_' . $user_id, 'user_latest_comment_count_' . $user_id]);
            $extra = [
                'user_latest_fans_count' => $arr[0] ?: 0,
                'user_latest_star_count' => $arr[1] ?: 0,
                'user_latest_comment_count' => $arr[2] ?: 0
            ];
        }
        if ($type == '1,7') {
            Redis::del('user_latest_star_count_' . $user_id);
        } else if($type == '3,4') {
            Redis::del('user_latest_comment_count_' . $user_id);
        } else if($type == '2') {
            Redis::del('user_latest_fans_count_' . $user_id);
        } else if($type == '5,6,8'){
            $page = \request('page', 1);
            if ($page == 1) {
                $first = $list->first();
                if ($first)
                    Redis::set('user_latest_notify_read_at_' . $user_id, strtotime($first->updated_at));
            }
        }
        return $this->paginate($list, $extra);
    }

    public function info(Request $request) {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $info = Notification::find(\request('id'));
        return $this->success($info);
    }

    public function unReadNum() {
        $user_id = $this->getUserId();
        $exists = false;
        if ($user_id) {
            $updated_at = Notification::where('is_send', 1)->whereIn('type', [5,6,8])->where(function ($q) use($user_id) {
                    $q->where('entity_user_id', $user_id)->orWhere('entity_user_id', 0);
                })->orderBy('id', 'desc')->value('updated_at') ?? 0;
            if ($updated_at)
                $updated_at = strtotime($updated_at);

            $last_read_updated_at = Redis::get('user_latest_notify_read_at_' . $user_id) ?: 0;

            if ($updated_at > $last_read_updated_at) {
                $exists = true;
            } else {
                $arr = Redis::mget(['user_latest_fans_count_' . $user_id, 'user_latest_star_count_' . $user_id, 'user_latest_comment_count_' . $user_id]);
                $user_latest_fans_count = $arr[0] ?: 0;
                $user_latest_star_count = $arr[1] ?: 0;
                $user_latest_comment_count = $arr[2] ?: 0;
                if ( ($user_latest_fans_count + $user_latest_star_count + $user_latest_comment_count) > 0 ) {
                    $exists = true;
                }
            }
        }
        return $this->success([
            'exists' => $exists,
        ]);
    }
}

